package com.hwhp.web.console.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.dispatcher.mapper.ActionMapping;

import com.hwhp.common.BaseAction;
import com.hwhp.entity.Deptment;
import com.hwhp.entity.Menu;
import com.hwhp.entity.Role;
import com.hwhp.entity.YzUser;
import com.hwhp.service.console.DepartService;
import com.hwhp.service.console.MenuService;
import com.hwhp.service.console.RoleService;
import com.hwhp.service.console.UserService;
import com.hwhp.service.console.impl.MenuServiceImpl;
import com.hwhp.service.console.impl.RoleServiceImpl;



public class AdminRole extends BaseAction {
	private RoleService roleService = null ;
	private MenuService menuService = null ;
	private UserService userService = null ;
	private DepartService departService = null ;
	/**
	 * 角色列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String roleList() {
		HttpServletRequest request = this.getHttpRequest();
		String search = request.getParameter("search");
		List<Role> list = roleService.getRoleList(search);
		for(Role role:list){
			
			String functionIdSet = role.getFunctionset();
			
			String nameSet = menuService.getAllMenuNameByIdSet(functionIdSet);
			System.out.println(nameSet);
			role.setFunctionset(nameSet);
		}
		request.setAttribute("roleList", list);
		request.setAttribute("search", search);
		//return mapping.findForward("rolemanager.list.success");
		return "rolemanager.list.success" ;
	}

	/**
	 * 角色编辑
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String roleEdit() {
		HttpServletRequest request = this.getHttpRequest();
		String roleid = request.getParameter("roleid");
		String act = request.getParameter("act");
		List<Menu> list=menuService.getAllSubMenuById("-1", "1");
		if (act != null && "add".equals(act)) {
			Role roleObj = new Role();
			request.setAttribute("roleObj", roleObj);
			request.setAttribute("act", "add");
		}
		if (act != null && "edit".equals(act)) {
			Role roleObj = roleService.getRoleById(roleid);
			String functionIdSet=roleObj.getFunctionset();
			for(Menu menu:list){
				String menuid=menu.getId();
				if(functionIdSet.indexOf(menuid)!=-1){
					menu.setIsused(2);
				}
			}
			request.setAttribute("roleObj", roleObj);
			request.setAttribute("act", "edit");
		}
		request.setAttribute("mentList", list);
		//return mapping.findForward("rolemanager.edit.success");
		return "rolemanager.edit.success" ;
	}

	/**
	 * 新增角色
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String insertUser() {
		boolean success = false;
		List<Menu> list=menuService.getAllSubMenuById("-1", "1");
		HttpServletRequest request = this.getHttpRequest();
		String act = request.getParameter("act");
		String roleId = request.getParameter("roleid");
		String roleName = request.getParameter("rolename");
		String roleCode = request.getParameter("rolecode");
		String isUsed = request.getParameter("isused");
		// String courseId = request.getParameter("courseId");
		String[] functionIdSet = request.getParameterValues("checkbox_id");
		String functionStr="";
		if(functionIdSet!=null&&!"".equals(functionIdSet)){
			for(int i=0;i<functionIdSet.length;i++){
				functionStr = functionStr+",'"+functionIdSet[i]+"'";
			}
			functionStr=functionStr.substring(1);
		}
		Role role = new Role();
		if ("add".equals(act)) {
			role.setRolename(roleName);
			role.setIsUsed(isUsed);
			role.setRolecode(roleCode);
			role.setFunctionset(functionStr);
			for(Menu menu:list){
				String menuid=menu.getId();
				if(functionStr.indexOf(menuid)!=-1){
					menu.setIsused(2);
				}
			}
			success = roleService.insertRole(role);
		}
		if ("edit".equals(act)) {
			role = roleService.getRoleById(roleId);
			role.setRolename(roleName);
			role.setIsUsed(isUsed);
			role.setRolecode(roleCode);
			role.setFunctionset(functionStr);
			String functionid = role.getFunctionset();
			for(Menu menu:list){
				String menuid=menu.getId();
				if(functionid.indexOf(menuid)!=-1){
					menu.setIsused(2);
				}
			}
			success = roleService.editRole(role);
		}
		request.setAttribute("mentList", list);
		request.setAttribute("roleObj",role);
		request.setAttribute("act", act);
		//return mapping.findForward("rolemanager.edit.success");
		return "rolemanager.edit.success"; 
	}

	/**
	 * 角色删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String deleteRole() {
		HttpServletRequest request = this.getHttpRequest() ;
		String roleId = request.getParameter("roleid");
		// String deptId = request.getParameter("deptid");
		boolean success = false;
		Role role = roleService.getRoleById(roleId);
		success = roleService.deleteRole(role);
		return this.roleList();
	}
	/**
	 * 角色权限分配初始匄
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String distributeRoleIndex() {
		List<Deptment> list = departService.getChildDepts("-1");
		HttpServletRequest request = this.getHttpRequest() ;
		request.setAttribute("deptList", list);
		List<Role> rolelist = roleService.getRoleList("");
		request.setAttribute("roleList", rolelist);
		//return mapping.findForward("distribute.init.success");
		return "distribute.init.success"; 
	}
	/**
	 * 人员权限列表显示
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String userDistributeList() {
		HttpServletRequest request = this.getHttpRequest();
		String deptid = request.getParameter("deptid");
		List<YzUser> list=new ArrayList();
		if (deptid == null || "".equals(deptid) || "null".equals(deptid)) {
			deptid = "-1";
		}else{
			list = userService.queryUserByDeptId(deptid);
		}
		System.out.println("deptId:#"+deptid);
		
		
		for(YzUser user:list){
			String roleNameSet="";
			String roleIdSet=user.getRole();
			if(roleIdSet!=null&&!"".equals(roleIdSet)){
			String[] roleSet=roleIdSet.split(",");
			for(int i=0;i<roleSet.length;i++){
				String roleId=roleSet[i];
				roleId=roleId.replaceAll("'", "");
				Role role= roleService.getRoleById(roleId);
				roleNameSet=roleNameSet+","+role.getRolename();
			}
			if(roleNameSet!=null&&!"".equals(roleNameSet)){
				roleNameSet=roleNameSet.substring(1);
			}
			}
			user.setRole(roleNameSet);
			
		}
		request.setAttribute("userList", list);
		request.setAttribute("deptid", deptid);
		//return mapping.findForward("distribute.list.success");
		return "distribute.list.success" ;
	}
	/**
	 * 角色权限编辑初始匄1�7
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String userDistributeEdit() {
		HttpServletRequest request = this.getHttpRequest();
		String userid=request.getParameter("userid");
		String useridset=request.getParameter("useridset");
		String selectState=request.getParameter("select");
		List<Role> usedlist = new ArrayList();
		List<Role> unuselist = new ArrayList();
		List<YzUser> userlist=new ArrayList();
		if(selectState!=null&&"one".equals(selectState)){
		if(userid!=null&&!"".equals(userid)){
			YzUser user=getUserService().getUser(userid);
			userlist.add(user);
			String roleIdSet=user.getRole();
		 if(roleIdSet!=null&&!"".equals(roleIdSet)){
				usedlist=roleService.getRoleListByIdSet(roleIdSet);
				unuselist=roleService.getUnRoleListByIdSet(roleIdSet);
			}else{
				unuselist=roleService.getRoleList("");
			}
		}else{
			unuselist=roleService.getRoleList("");
		}
		}
		
		
		if(selectState!=null&&"all".equals(selectState)){
			userlist= userService.getUserListByIdSet(useridset);
			unuselist = roleService.getRoleList("");
		}
		request.setAttribute("usedlist", usedlist);
		request.setAttribute("unuselist", unuselist);
		request.setAttribute("userlist", userlist);
		//return mapping.findForward("distribute.edit.success");
		return "distribute.edit.success" ;
	}
	/**
	 * 角色权限分配保存
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String distributeRoleSave() {
		HttpServletRequest request = this.getHttpRequest();
		String roleidSet=request.getParameter("roleidSet");
		String useridSet=request.getParameter("useridSet");
		String unroleidSet=request.getParameter("unroleidSet");
		
		 List<YzUser> userlist=new ArrayList();
		 List<Role> usedlist = new ArrayList();
		 List<Role> unuselist = new ArrayList();
		String success="true";
		try{
		 if(useridSet!=null&&!"".equals(useridSet)){
			   userlist= userService.getUserListByIdSet(useridSet);
				for(YzUser userobj:userlist){
					userobj.setRole(roleidSet);
					getUserService().editUser(userobj);
				 }
				if(roleidSet!=null&&!"".equals(roleidSet)){
					usedlist= roleService .getRoleListByIdSet(roleidSet);
				}
				if(unroleidSet!=null&&!"".equals(unroleidSet)){
				   unuselist= roleService.getRoleListByIdSet(unroleidSet);
				}
			}
		}catch(Exception e){
			success="false";
		}
		request.setAttribute("usedlist", usedlist);
		request.setAttribute("unuselist", unuselist);
		request.setAttribute("userlist", userlist);
		request.setAttribute("success", success);
		//return mapping.findForward("distribute.edit.success");
		return "distribute.edit.success" ;
	}


	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setDepartService(DepartService departService) {
		this.departService = departService;
	}

	public DepartService getDepartService() {
		return departService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	public RoleService getRoleService(RoleService roleService){
		return roleService ;
	}
}
